package com.example.haohao.myview.mvp2;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by DELL on 2018/7/11.
 */

public class RetrofitManager {

    //设置公共的请求url
    private static String BASE_URL = "https://www.zhaoapi.cn/";
    private final Retrofit retrofit;

//    private static class SingleHolder {
//        private static final RetrofitManager _INSTANCE = new RetrofitManager(BASE_URL);
//    }

        private static final RetrofitManager _INSTANCE = new RetrofitManager(BASE_URL);


    public static RetrofitManager getDefault() {

        return _INSTANCE;

    }

    private RetrofitManager(String baseUrl) {

        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(buildOkhttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

    }

    private OkHttpClient buildOkhttpClient() {

        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(5000, TimeUnit.MILLISECONDS)
                .writeTimeout(5000, TimeUnit.MILLISECONDS)
                .connectTimeout(5000, TimeUnit.MILLISECONDS)
                .build();

        return client;
    }

    public <T> T create(Class<T> Clazz) {

        return retrofit.create(Clazz);

    }
}

//        ————————————————
//        版权声明：本文为CSDN博主「GaoYyyy」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
//        原文链接：https://blog.csdn.net/GaoYiranBlog/java/article/details/81003815
